package cn.bellychang.leetcode.question024;

/**
 * @author ChangLiang
 * @date 2021/3/16
 */
public class Solution2 {



    public ListNode swapPairs(ListNode head) {

        if (head == null) {
            return null;
        }

        ListNode dummyHead = new ListNode(-1);
        dummyHead.next = head;
        ListNode tail = dummyHead;
        ListNode curr = head;
        ListNode next = curr.next;

        while (curr != null && next != null) {
            // 有两个工作 dummy-->@1461-->@1462-->@1478-->@1479
            // 1. 将@1461与@1478连接起来
            // 2. 进行翻转动作 @1461<--@1462

            curr.next = next.next;
            next.next = curr;
            tail.next = next;

            tail = curr;

            curr = curr.next;
            if (curr == null) {
                break;
            }
            next = curr.next;
        }

        return dummyHead.next;
    }
}
